package com.servlet;

import com.bean.Message;
import com.bean.Notice;
import com.bean.Student;
import com.bean.Teacher;
import com.google.gson.Gson;
import com.service.MessageService;
import com.service.impl.MessageServiceImpl;
import com.utils.DateUtils;
import com.utils.Page;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.utils.Page.PAGE_SIZE;

@WebServlet(name = "MessageServlet", urlPatterns = "/MessageServlet.do")
public class MessageServlet extends BaseServlet {
    MessageService messageServlet = new MessageServiceImpl();
    MessageService messageService=new MessageServiceImpl();

    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), PAGE_SIZE);
        String content = request.getParameter("content");
        Page<Message> page = messageService.page(pageNo, pageSize, content);
        Map map = new LinkedHashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", page.getPageTotalCount());
        map.put("data", page.getItems());
        response.getWriter().write(WebUtils.parseJson(map));
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Message message = new Message();
        Student student = (Student) request.getSession().getAttribute("user");
        Map<String, Object> map = new LinkedHashMap<>();
        message.setStudentId(student.getId());
        message.setCreateTime(DateUtils.getDate());
        WebUtils.copyParamToBean(request.getParameterMap(), message);
        message.setId(null);
        int tip = messageServlet.insert(message);
        String msg = "添加失败！";
        if (tip == 0) {
            map.put("tip", 1);
            msg = "添加成功！";
        }
        map.put("msg", msg);
        response.getWriter().write(WebUtils.parseJson(map));
    }

    protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Map<String, Object> map = new LinkedHashMap<>();
        int tip = messageService.deleteById(id);
        String msg = "删除失败！";
        if (tip == 0) {
            map.put("tip", 1);
            msg = "删除成功！";
        }
        map.put("msg", msg);
        response.getWriter().write(WebUtils.parseJson(map));
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Message message = new Message();
        Map<String, Object> map = new LinkedHashMap<>();
        WebUtils.copyParamToBean(request.getParameterMap(), message);
        int tip = messageService.updeteInfo(message);
        String msg = "修改失败！";
        if (tip == 0) {
            map.put("tip", 1);
            msg = "修改成功！";
        }
        map.put("msg", msg);
        response.getWriter().write(WebUtils.parseJson(map));
    }
}
